angular.controller(ꞌct_reactionsꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 118

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 118
rs 8.2857
nop 7

12 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 3 1
A ��) 0 5 1
A ��) 0 5 2
A ��) 0 10 3
A ��) 0 12 2
A ��) 0 11 2
A ��) 0 7 3
A ��) 0 3 1
A ��) 0 12 4
A ��) 0 3 1
A ��) 0 10 2
B ��) 0 13 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 reactions
3
 Component that handles reactions and molecules.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('reactions', {
10
  templateUrl: 'views/reactions.html',
11
  controller:  'ct_reactions',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_reactions', ['state', 'data', 'visibility', 'util', 'format', 'reaction', 'upgrade',
16
function (state, data, visibility, util, format, reactionService, upgradeService) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.format = format;
22
  ct.upgradeService = upgradeService;
23
  ct.adjustAmount = [1, 10, 25, 100];
24
25
  function update(player) {
26
    for(let slot of player.element_slots){
27
      if(!slot){
28
        continue;
29
      }
30
      for (let reaction of slot.reactions) {
31
        if (!reaction.active) {
32
          continue;
33
        }
34
        state.reactions.push({number: numberToReact(player, reaction.reaction), reaction: reaction.reaction});
35
      }
36
    }
37
  }
38
39
  function numberToReact(player, reaction) {
40
    let power = util.calculateValue(data.global_upgrades.reaction_bandwidth.power.base,
41
            data.global_upgrades.reaction_bandwidth.power,
42
            player.global_upgrades_current.reaction_bandwidth);
43
    let number = power;
44
    for(let resource in reaction.reactants){
45
      number = Math.min(number, player.resources[resource]);
46
    }
47
    return number;
48
  }
49
50
  ct.reactionSize = function (player) {
51
    let size = 0;
52
    for(let slot of player.element_slots){
53
      if(!slot){
54
        continue;
55
      }
56
      size += slot.reactions.length;
57
    }
58
    return size;
59
  };
60
61
  /* Adds a new reaction to the player list */
62
  ct.addReaction = function (player, slot, key) {
63
    if(ct.reactionSize(player) >= util.calculateValue(data.global_upgrades.reaction_slots.power.base,
64
            data.global_upgrades.reaction_slots.power,
65
            player.global_upgrades.reaction_slots)){
66
      return;
67
    }
68
    let reaction = data.reactions[key];
69
    slot.reactions.push({
70
      active: false,
71
      reaction: angular.copy(reaction)
72
    });
73
  };
74
75
  ct.removeReaction = function (slot, item) {
76
    for(let i = 0; i < slot.reactions.length; i++){
77
      if(slot.reactions[i] === item){
78
        slot.reactions.splice(i, 1);
79
      }
80
    }
81
  };
82
83
  ct.checkAll = function(slot){
84
    for(let reaction of slot.reactions){
85
      reaction.active = slot.active;
86
    }
87
  }
88
89
  ct.visibleReactions = function(slot) {
90
    return slot.reactions;
91
  };
92
93
  ct.availableReactions = function(slot, player) {
94
    return visibility.visible(data.reactions, isReactionAvailable, slot.element, null, player);
95
  };
96
97
  function isReactionAvailable(entry, currentElement, player) {
98
    let available = true;
99
    let reaction = data.reactions[entry];
100
    for(let resource in reaction.reactant){
101
      available = available && player.resources[resource] !== null;
102
    }
103
    // Workaround to reuse the visibility function. It expects an object with the
104
    // reaction inside
105
    let reactionObject = {reaction:reaction};
106
    return available && isReactionVisible(reactionObject, currentElement);
107
  }
108
109
  function isReactionVisible(entry, currentElement) {
110
    let reaction = entry.reaction;
111
    if(reaction.elements.length === 0){
112
      return true;
113
    }
114
    for(let element of reaction.elements){
115
      if(element === currentElement){
116
        return true;
117
      }
118
    }
119
    return false;
120
  }
121
122
  ct.adjustLevel = function(player, upgrade, amount){
123
    player.global_upgrades_current[upgrade] += amount;
124
    // We cap it between 1 and the current max level
125
    player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade]));
126
  };
127
128
  ct.visibleUpgrades = function() {
129
    return visibility.visible(data.global_upgrades, upgradeService.filterByTag('reactions'));
130
  };
131
132
  state.registerUpdate('reactions', update);
133
}]);
134